<?php

namespace App\Http\Middleware\Admin;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class CheckLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        if(!array_key_exists('token',$request->header())){

            return response()->json(['code'=>400,'msg'=>'非法token，禁止操作！'],400);
        }
        $data=Redis::get($request->header('token'));
        if(empty($data)){
            return response()->json(['code'=>400,'msg'=>'token已失效，禁止操作！'],400);
        }
        $request->adminId=$data;
        return $next($request);
    }
}
